/*
 * @Date: 2023-02-10 22:12:58
 * @LastEditTime: 2023-03-06 14:03:52
 * @FilePath: /xing-qiu-da-zhan/src/language/index.ts
 * 介绍:
 */
import { createI18n } from "vue-i18n";
// 引入同级目录下文件
const modules = import.meta.glob("./*", { eager: true });

function getLangAll(): any {
  let message: any = {};
  getLangFiles(modules, message);
  return message;
}
/**
 * 获取所有语言文件
 * @param {Object} mList
 */
function getLangFiles(mList: any, msg: any) {
  for (let path in mList) {
    if (mList[path].default) {
      //  获取文件名
      const before = path.lastIndexOf("/") + 1;
      const after = path.lastIndexOf(".");
      let pathName = path.slice(before, after);

      if (msg[pathName]) {
        msg[pathName] = {
          ...mList[pathName],
          ...mList[path].default,
        };
      } else {
        msg[pathName] = mList[path].default;
      }
    }
  }
}
export const languageList = getLangAll();

//注册i8n实例并引入语言文件
export const i18n = createI18n({
  legacy: false,
  globalInjection: true,
  global: true,
  // locale: "en-CH", // 默认语言
  // locale: "zh-HANT", // 默认语言
  locale: "zh-CN", // 默认语言
  fallbackLocale: "zh-CN", // 回退语言
  messages: languageList,
});
